package com.hzhjhh.view.manageView.menus.storage.inReturnGoods;

import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.constant.DateFormatContants;
import com.hzhjhh.entity.BackSale;
import com.hzhjhh.init.dialog.Mytable;
import com.hzhjhh.mapper.BackSaleMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.storage.InReturnGoodsDialog;
import com.hzhjhh.view.manageView.menus.storage.constant.ShowConstants;

import cn.hutool.core.date.DateUtil;

/**
 * 菜单栏初始化-仓库-进/退货-查询按钮
 * 
 * @author 12083
 *
 */
public class QueryBt {
	
	private static BackSaleMapper backSaleMapper;

	/**
	 * 按钮
	 */
	public static JButton btInit(InReturnGoodsDialog inReturnGoodsDialog) {
		
		
		JButton query = new JButton();
		query.setBounds(476, 436, 81, 30);
        query.setText("查询");
        query.setToolTipText("");
        query.addActionListener(event -> {
        	JScrollPane jScrollPane1 = inReturnGoodsDialog.getJScrollPane1();
        	Vector vector1 = inReturnGoodsDialog.getVector1();
        	JComboBox jComboBox1 = inReturnGoodsDialog.getJComboBox1();
        	JTextField jTextField2 = inReturnGoodsDialog.getTxfMerchandiseCode();
        	JTable jTable1 = inReturnGoodsDialog.getJTable1();
        	List<BackSale> backSaleList = backSaleMapper.selectById(jTextField2.getText(), null);
        	if (CollectionUtils.isEmpty(backSaleList)) {
				return;
			}
        	Vector showV = new Vector<>();
        	for (BackSale backSale : backSaleList) {
				Vector rowV = new Vector<>();
				rowV.add(backSale.getBackProductId());
				rowV.add(backSale.getBackProductName());
				rowV.add(backSale.getBackNumber());
				rowV.add(backSale.getPrice());
				rowV.add(DateUtil.format(backSale.getBackDate(), DateFormatContants.YYYY_MM_DD_HH_MM_SS));
				rowV.add(backSale.getPrice());
				
				showV.add(rowV);
			}
        	jTable1 = Mytable.maketable(showV, ShowConstants.IN_RETURN_GOODS_HEAD_NAME); //显示内容
            jScrollPane1.getViewport().add(jTable1); //把表装入容器
//        	if (jComboBox1.getSelectedItem().equals("  进  货 ")) {
//                Vector selectData = new Vector();
//                selectData = Select_Means.Select_inoutSale(jTextField2.getText(),
//                        "1");
//                int i = 0;
//                vector1.removeAllElements();
//                while (i < selectData.size()) {
//                    Vector select1 = new Vector();
//                    Vector selectData1 = new Vector();
//                    select1 = (Vector) selectData.get(i);
//
//                    selectData1.add(select1.get(0).toString());
//                    selectData1.add(select1.get(1).toString());
//                    selectData1.add(select1.get(2).toString());
//                    selectData1.add(select1.get(3).toString());
//                    selectData1.add(select1.get(4).toString());
//                    Double num1 = new Double(selectData1.get(2).toString());
//                    Double num2 = new Double(selectData1.get(3).toString());
//                    double num3 = ((int) (num2 * num1 * 100)) / 100.0;
//                    Double num4 = new Double(num3);
//                    selectData1.add(num4.toString());
//                    i++;
//                    vector1.add(selectData1);
//                }
//
//                jTable1 = Mytable.maketable(vector1, ShowConstants.IN_RETURN_GOODS_HEAD_NAME); //显示内容
//                jScrollPane1.getViewport().add(jTable1); //把表装入容器
//            }
//            if (jComboBox1.getSelectedItem().equals("  退  货 ")) {
//                Vector selectData = new Vector();
//                selectData = Select_Means.Select_inoutSale(jTextField2.getText(),
//                        "0");
//                int i = 0;
//                vector1.removeAllElements();
//                while (i < selectData.size()) {
//                    Vector select1 = new Vector();
//                    Vector selectData1 = new Vector();
//                    select1 = (Vector) selectData.get(i);
//                    selectData1.add(select1.get(0).toString());
//                    selectData1.add(select1.get(1).toString());
//                    selectData1.add(select1.get(2).toString());
//                    selectData1.add(select1.get(3).toString());
//                    selectData1.add(select1.get(4).toString());
//                    Double num1 = new Double(selectData1.get(2).toString());
//                    Double num2 = new Double(selectData1.get(3).toString());
//                    double num3 = ((int) (num2 * num1 * 100)) / 100.0;
//                    Double num4 = new Double(num3);
//                    selectData1.add(num4.toString());
//                    i++;
//                    vector1.add(selectData1);
//                }
//
//                jTable1 = Mytable.maketable(vector1, ShowConstants.IN_RETURN_GOODS_HEAD_NAME); //显示内容
//                jScrollPane1.getViewport().add(jTable1); //把表装入容器
//
//            }
        });
        
        // 初始化mapper
        SqlSession sqlSession = DBUtils.getSqlSession(true);
        backSaleMapper = sqlSession.getMapper(BackSaleMapper.class);
        
		return query;
	}
}
